﻿namespace Sidvall.Data
{
    public static class EntityManager
    {
        #region AcceptItemChanges

        public static void AcceptItemChanges(IDataRowContextProvider value, Sidvall.Data.AcceptChangesSettings acceptChangesSettings)
        {
            if (value == null)
                return;
            if (acceptChangesSettings == null)
                return;
            switch (acceptChangesSettings.DataRowContextOption)
            {
                case DataRowContextOption.State:
                    value.SetDataRowContext(null, Sidvall.Data.DataRowState.Unchanged);
                    break;
                case DataRowContextOption.StateAndExistingKey:
                case DataRowContextOption.StateAndKey:
                    value.SetDataRowContext(null, Sidvall.Data.DataRowState.Unchanged, null);
                    break;
                default:
                    break;
            }
        }

        #endregion
        #region AfterImportItem

        public static void AfterImportItem(IDataRowContextProvider destination, IDataRowContextProvider source, Sidvall.Data.CopySettings copySettings)
        {
            if (destination == null)
                return;
            if (source == null)
                return;
            if (copySettings == null)
                return;
            switch (copySettings.DataRowContextOption)
            {
                case DataRowContextOption.State:
                    destination.SetDataRowContext(source, copySettings.DataRowState);
                    break;
                case DataRowContextOption.StateAndExistingKey:
                    destination.SetDataRowContext(source, copySettings.DataRowState, source.DataRowContextKey);
                    break;
                case DataRowContextOption.StateAndKey:
                    EnsureDataRowContextKey(source);
                    destination.SetDataRowContext(source, copySettings.DataRowState, source.DataRowContextKey);
                    break;
                default:
                    if (copySettings.DataRowState != null)
                        destination.SetDataRowContext(null, copySettings.DataRowState);
                    break;
            }
            if (copySettings.RaisePropertyChanged)
                ((Sidvall.Business.IOnPropertyChanged)destination).OnPropertyChanged(null, true, false);
        }

        #endregion
        #region EnsureDataRowContextKey

        public static void EnsureDataRowContextKey(IDataRowContextProvider item)
        {
            if (item == null)
                throw new System.ArgumentNullException(nameof(item));

            if (item.DataRowContextStorageKey == null)
            {
                if (string.IsNullOrWhiteSpace(item.DataRowContextKey))
                    item.DataRowContextKey = "New:" + System.Guid.NewGuid().ToString();
            }
        }

        #endregion
        #region OnPropertyChanged

        public static void OnPropertyChanged(object item, string propertyName, bool raisePropertyChanged, bool updateState)
        {
            Sidvall.Business.IOnPropertyChanged onPropertyChanged;

            if (raisePropertyChanged || updateState)
            {
                onPropertyChanged = item as Sidvall.Business.IOnPropertyChanged;
                if (onPropertyChanged != null)
                    onPropertyChanged.OnPropertyChanged(propertyName, raisePropertyChanged, updateState);
            }
        }

        #endregion
    }
}
